package cn.edu.csu.smproject;


import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * 在XML文档中实现增、删、改、查
 * @author TigerLee
 *
 */
public class HandleXML {

    /**
     * 采用DOM方式
     *
     * @author TigerLee
     */
    public class DOMForXml {
        /**
         * 获得doc对象
         *
         * @param fileName
         * @return
         */
        public Document getDocument(String fileName) {
            Document document = null;
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                document = builder.parse(new File(fileName));

            } catch (Exception e) {
                e.printStackTrace();
            }

            return document;
        }

        /**
         * 将改动持久到文件
         *
         * @param doc
         * @param distFileName
         */
        public void modifyFile(Document doc, String distFileName) {
            try {
                TransformerFactory tf = TransformerFactory.newInstance();
                Transformer tfer = tf.newTransformer();
                DOMSource dsource = new DOMSource(doc);
                StreamResult sr = new StreamResult(new File("student.xml"));
                tfer.transform(dsource, sr);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        /**
         * 解析
         *
         * @param fileName
         */
        public void paseXml(String fileName) {
            Document document = getDocument(fileName);
            NodeList nodeList = document.getElementsByTagName("student");
            for (int i = 0; i < nodeList.getLength(); i++) {
                StringBuilder sb = new StringBuilder();
                sb.append("姓名：" + document.getElementsByTagName("name").item(i).getFirstChild().getNodeValue());
                sb.append(" , ");
                sb.append("年龄：" + document.getElementsByTagName("age").item(i).getFirstChild().getNodeValue());
                sb.append(" , ");
                sb.append("性别：" + document.getElementsByTagName("sex").item(i).getFirstChild().getNodeValue());
                sb.append(" , ");
                sb.append("地址：" + document.getElementsByTagName("address").item(i).getFirstChild().getNodeValue());

                System.out.println(sb.toString());
            }
        }

        /**
         * 创建一个新的学生
         *
         * @param name
         * @param age
         * @param sex
         * @param address
         */
        public void addNewNode(String name, String age, String sex, String address) {
            try {
                Document document = getDocument("student.xml");
                NodeList nodeList = document.getElementsByTagName("students");
                //创建新的节点
                Node studentNode = document.createElement("student");
                Node nameNode = document.createElement("name");
                nameNode.appendChild(document.createTextNode(name));
                Node ageNode = document.createElement("age");
                ageNode.appendChild(document.createTextNode(age));
                Node sexNode = document.createElement("sex");
                sexNode.appendChild(document.createTextNode(sex));
                Node addressNode = document.createElement("address");
                addressNode.appendChild(document.createTextNode(address));
                // 添加节点
                studentNode.appendChild(nameNode);
                studentNode.appendChild(ageNode);
                studentNode.appendChild(sexNode);
                studentNode.appendChild(addressNode);
                nodeList.item(0).appendChild(studentNode);
                // 此时真正的处理将新数据添加到文件中（磁盘）
                TransformerFactory tf = TransformerFactory.newInstance();
                Transformer tfer = tf.newTransformer();
                DOMSource dsource = new DOMSource(document);
                StreamResult sr = new StreamResult(new File("student.xml"));
                tfer.transform(dsource, sr);
            } catch (Exception e) {
                e.printStackTrace();
            }

            paseXml("student.xml");
        }

        /**
         * 删除一个节点
         *
         * @param name
         */
        public void deleteNode(String name) {
            Document document = getDocument("student.xml");

            NodeList nodeList = document.getElementsByTagName("name");
            for (int i = 0; i < nodeList.getLength(); i++) {
                String value = nodeList.item(i).getFirstChild().getTextContent();
                if (name != null && name.equalsIgnoreCase(value)) {
                    Node parentNode = nodeList.item(i).getParentNode();
                    document.getFirstChild().removeChild(parentNode);
                }
            }
            modifyFile(document, "student.xml");
        }

        /**
         * 根据name修改某个节点的内容
         *
         * @param name
         */
        public void updateNode(String name) {

            Document document = getDocument("student.xml");
            NodeList nodeList = document.getElementsByTagName("name");
            for (int i = 0; i < nodeList.getLength(); i++) {
                String value = nodeList.item(i).getFirstChild().getTextContent();
                if (name != null && name.equalsIgnoreCase(value)) {
                    Node parentNode = nodeList.item(i).getParentNode();
                    NodeList nl = parentNode.getChildNodes();
                    for (int j = 0; j < nl.getLength(); j++) {
                        String modifyNode = nl.item(j).getNodeName();
                        if (modifyNode.equalsIgnoreCase("age")) {
                            nl.item(j).getFirstChild().setTextContent("25");
                        }
                    }
                }
            }
            modifyFile(document, "student.xml");
        }
    }
    public  Document getD(String f){
        DOMForXml domForXml =  new DOMForXml();
        return domForXml.getDocument(f);
    }
    public static void main(String[] args) {
        HandleXML handleXML = new HandleXML();
        Document document = handleXML.getD("2_1.xml");
        NodeList nodeList = document.getElementsByTagName("c:Classes");
        Node n = nodeList.item(0);
        NodeList l = n.getChildNodes();
        for (int i=1;i<l.getLength()-1;i=i+2){
//            System.out.println(l.item(i).getChildNodes().item(3).getTextContent())


        }
//        System.out.println(l.item(1).getTextContent());
//        System.out.println(nodeList.getLength());
//        for (int i = 0; i < nodeList.getLength(); i++) {
//            StringBuilder sb = new StringBuilder();
//            sb.append("姓名：" + document.getElementsByTagName("name").item(i).getFirstChild().getNodeValue());
//            sb.append(" , ");
//            sb.append("年龄：" + document.getElementsByTagName("age").item(i).getFirstChild().getNodeValue());
//            sb.append(" , ");
//            sb.append("性别：" + document.getElementsByTagName("sex").item(i).getFirstChild().getNodeValue());
//            sb.append(" , ");
//            sb.append("地址：" + document.getElementsByTagName("address").item(i).getFirstChild().getNodeValue());
//
//            System.out.println(sb.toString());
//        }
    }
}